package com.tiza.xiaoyoubao.net;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tianze.library.adapter.gson.DoubleTypeAdapter;
import com.tianze.library.adapter.gson.IntegerTypeAdapter;
import com.tianze.library.adapter.gson.LongTypeAdapter;
import com.tianze.library.adapter.gson.StringTypeAdapter;
import com.tianze.library.utils.L;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import okhttp3.Response;

/**
 * Created by mapple on 17/2/13.
 */

public abstract class JsonCallback<T> extends Callback<ResponseInfo<T>> {

    private static final Gson gson;

    static {
        gson = new GsonBuilder().registerTypeAdapter(int.class, new IntegerTypeAdapter())
                                .registerTypeAdapter(Integer.class, new IntegerTypeAdapter())
                                .registerTypeAdapter(long.class, new LongTypeAdapter())
                                .registerTypeAdapter(Long.class, new LongTypeAdapter())
                                .registerTypeAdapter(double.class, new DoubleTypeAdapter())
                                .registerTypeAdapter(Double.class, new DoubleTypeAdapter())
                                .registerTypeAdapter(String.class, new StringTypeAdapter())
                                .create();
    }

    private Class<T> entityClass;
    private boolean getFirst;

    public JsonCallback(Class<T> prmEntityClass) {
        this.entityClass = prmEntityClass;
    }

    public JsonCallback(Class<T> prmEntityClass, boolean prmGetFirst) {
        this.entityClass = prmEntityClass;
        this.getFirst = prmGetFirst;
    }

    @Override
    public ResponseInfo<T> parseNetworkResponse(Response response) throws Exception {
        String _json = response.body().string();
        L.print(_json);
        JSONObject object = new JSONObject(_json);
        int _code = object.getInt("Code");
        String _message = object.getString("Message");
        List<T> _data = null;
        Map<String, Object> _map = null;
        try {
            JSONArray _dataJson = object.getJSONArray("Data");
            if (null != _dataJson && _dataJson.length() > 0) {
                if (getFirst) {
                    _map = new HashMap<>();
                    JSONObject _object = _dataJson.getJSONObject(0);
                    JSONArray _array = _object.names();
                    Iterator<String> _keys = _object.keys();
                    while (_keys.hasNext()) {
                        String _key = _keys.next();
                        Object _value = _object.get(_key);
                        _map.put(_key, _value);
                    }
                } else {
                    _data = new ArrayList<T>();
                    for (int i = 0; i < _dataJson.length(); i++) {
                        String _jsonString = _dataJson.getString(i);
                        _data.add(gson.fromJson(_jsonString, entityClass));
                    }
                }
            }
        } catch (JSONException e) {
            L.e("JsonCallback", e.getLocalizedMessage());
        }
        ResponseInfo<T> _model = new ResponseInfo<>();
        _model.setCode(_code);
        _model.setMessage(_message);
        _model.setFirstEntity(_map);
        _model.setData(_data);
        return _model;
    }
}
